home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / CrashBurn.sit / Crash & Burn / source code / SerialMain.cpp < prev    next >
C/C++ Source or Header  |  1997-06-26  |  3KB  |  156 lines

  1. #include "patches.h"
  2. #include <traps.h>
  3. #include <lowmem.h>
  4. #include "MacsBugSerialStuff.h"
  5.  
  6.  
  7. static asm long*    GetOldDebugUtilStorage()
  8. {
  9.     lea        _oldDebugUtilStorage,a0
  10.     rts
  11. _oldDebugUtilStorage:
  12.     dc.l    0x00000000
  13. }
  14.  
  15. static asm long* GetCountStorage()
  16. {
  17.     lea        _countStorage,a0
  18.     rts
  19. _countStorage:
  20.     dc.l    0x00000000
  21. }
  22.  
  23.  
  24. static asm long* GetValueStorage()
  25. {
  26.     lea        _valueStorage,a0
  27.     rts
  28. _valueStorage:
  29.     dc.l    0x00000000
  30. }
  31.  
  32.  
  33. static asm long* GetA5Storage()
  34. {
  35.     lea        _a5Store,a0
  36.     rts
  37. _a5Store:
  38.     dc.l    0x00000000
  39. }
  40.  
  41.  
  42. static Boolean        KeyIsDown(
  43.     short            theKeyCode)
  44. {
  45.     KeyMap            theKeys;
  46.     
  47.     GetKeys( theKeys);                    /* Get state of each key            */
  48.                                         
  49.         /* Ordering of bits in a KeyMap is truly bizarre. A KeyMap is a    */
  50.         /* 16-byte (128 bits) array where each bit specifies the start    */
  51.         /* of a key (0 = up, 1 = down). We isolate the bit for the        */
  52.         /* specified key code by first determining the byte position in    */
  53.         /* the KeyMap and then the bit position within that byte.        */
  54.         /* Key codes 0-7 are in the first byte (offset 0 from the        */
  55.         /* start), codes 8-15 are in the second, etc. The BitTst() trap    */
  56.         /* counts bits starting from the high-order bit of the byte.    */
  57.         /* For example, for key code 58 (the option key), we look at    */
  58.         /* the 8th byte (7 offset from the first byte) and the 5th bit    */
  59.         /* within that byte.                                            */
  60.         
  61.     return( BitTst( ((char*) &theKeys) + theKeyCode / 8,
  62.                     (long) 7 - (theKeyCode % 8) ) );
  63. }
  64.  
  65. // 16777216
  66.  
  67. static void memset(Ptr p,char value,long length)
  68. {
  69.     long    z = 0;
  70.     for(z = 0;z<length;z++){
  71.         p[z] = value;
  72.     }
  73. }
  74.  
  75. static    Ptr GetScreenBase()
  76. {
  77.     GDHandle        h = LMGetMainDevice();
  78.     
  79.     return h[0]->gdPMap[0]->baseAddr;
  80. }
  81.  
  82. static long    Poll(short selector)
  83. {
  84.     long        continueQ = true;
  85.     long*        theA5 = GetA5Storage();
  86.     
  87.     SetA5(*theA5);
  88.     
  89.     if(selector == 1){    // debugger enter
  90.         SerialDriver_SendBytes((unsigned char*)"Yo Dude!",8);
  91.     }
  92.     
  93.     long*        value = GetValueStorage();
  94.     
  95.     if(SerialDriver_BytePresent()){
  96.         UInt8    theByte;
  97.         
  98.         
  99.         if(SerialDriver_ReceiveByte(&theByte,10) == noErr){
  100.             if(theByte == 'a'){
  101.                 SerialDriver_SendBytes((unsigned char*)"eat me!",7);
  102.             }
  103.         }
  104.  
  105.  
  106.     }
  107.         
  108.     return continueQ;
  109. }
  110.  
  111.  
  112. static asm void newDebugUtil()
  113. {
  114.     subq.l    #4,a7                        // reserve space for old trap address
  115.     move.l    a0,-(a7)                    // save a0
  116.     movem.l    a1-a5/d0-d7,-(a7)            // save everything else
  117.     
  118.     move.w    d0,-(a7)
  119.     jsr        Poll
  120.     addq.l    #2,a7
  121.     tst.w    d0
  122.     bne.s    _Continue
  123.     
  124.     movem.l    (a7)+,a1-a5/d0-d7
  125.     move.l    (a7)+,a0
  126.     addq.l    #4,a7
  127.     rts
  128.     
  129. _Continue:
  130.     
  131.  
  132.     movem.l    (a7)+,a1-a5/d0-d7            // restore registers
  133.     
  134.     jsr        GetOldDebugUtilStorage        // get original trap address storage in a0
  135.     move.l    (a0),a0                        // get original trap address value into a0
  136.     move.l    a0,4(a7)                    // stuff old trap onto stack
  137.     move.l    (a7)+,a0                    // restore original a0
  138.     rts                                    // rts to jump to old trap.
  139. }
  140.  
  141. void main()
  142. {
  143.  
  144.     long*        theA5 = GetA5Storage();
  145.     *theA5 = SetCurrentA5();
  146.  
  147.     long*        debugUtilStorage = GetOldDebugUtilStorage();
  148.     *debugUtilStorage = PatchTrap(_DebugUtil,(long)newDebugUtil);
  149.     
  150.     
  151.     SerialDriver_Open();
  152.     SerialDriver_SendBytes((unsigned char*)"Go!",3);
  153.     
  154.     Debugger();
  155.  
  156. }